package admin

import (
	"duoke/context"
	"duoke/utils/orm"
	"duoke/utils/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"html/template"
	"net/http"
)

var Index = new(indexCtl)

type indexCtl struct{}

func (ctl *indexCtl) Index(c *gin.Context) {
	t, err := template.ParseFiles("./public/static/admin/index.html")
	if err != nil {
		c.String(http.StatusOK, "模板不存在")
		return
	}
	// 渲染模板
	err = t.Execute(c.Writer, gin.H{"api_url": context.Global.Domain})
	if err != nil {
		c.String(http.StatusOK, "模板解析失败")
	}
}
func (ctl *indexCtl) TruncateTable(c *gin.Context) {
	notTable := []string{"account", "account_setting", "area"}
	tableName, _ := orm.Db.DBMetas()
	for _, item := range tableName {
		// 如果item.name 再notTable 里面则跳过
		skip := false
		for _, skipTable := range notTable {
			if item.Name == skipTable {
				skip = true
				break
			}
		}
		if skip {
			continue
		}
		sprintf := fmt.Sprintf("truncate table `%s`", item.Name)
		exec, err := orm.Db.Exec(sprintf)
		if err != nil {
			logrus.Error(exec, err)
		}
	}
	response.Success(c, "清空成功", nil)
}
